package com.aneliya.framework.mvc.view;

import java.util.Hashtable;

public class View implements IView {
	
	private static View instance;
	public static View getInstance() {
		if (instance == null) {
			instance = new View();
		}
		return instance;
	}

	private Hashtable<String, Mediator> mediatorsRegister;
	
	protected View() {
		mediatorsRegister = new Hashtable<String, Mediator>();
	}

	public void registerMediator(Mediator mediator, String name) throws Exception {
		if (!mediatorsRegister.containsKey(name)) {
			if (mediator.name == null) {
				mediator.name = name;
				mediatorsRegister.put(name, mediator);
			} else if (mediator.name != name) {
				if (mediatorsRegister.containsKey(mediator.name)) {
					throw new Exception("This mediator is already registered under name: " + mediator.name);
				} else {
					mediator.name = name;
					mediatorsRegister.put(name, mediator);
				}
			} else {
				mediatorsRegister.put(name, mediator);
			}
		} else {
			throw new Exception("There is already a registered mediator for name: " + name);
		}
	}

	public void removeMediator(String name) throws Exception {
		if (mediatorsRegister.containsKey(name)) {
			mediatorsRegister.remove(name);
		} else {
			throw new Exception("There is no registered mediator for name: " + name);
		}
	}

	public Mediator retrieveMediator(String name) throws Exception {
		if (mediatorsRegister.containsKey(name)) {
			return mediatorsRegister.get(name);
		} else {
			throw new Exception("There is no registered mediator for name: " + name);
		}
	}

}
